Blockly.Blocks['create_tcp_socket'] = {
    init: function() {
        this.appendDummyInput()
            .appendField("创建TCP套接字");
        this.setOutput(true, "Socket"); // 设置输出为 Socket 类型
        this.setColour(color.networking);
        this.setTooltip("创建一个基于TCP的网络套接字");
        this.setHelpUrl("");
    }
};

Blockly.Python['create_tcp_socket'] = function(block) {
    Blockly.Python.definitions_['import_socket'] = 'import socket';
    var code = 'socket.socket(socket.AF_INET, socket.SOCK_STREAM)';
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Blocks['create_udp_socket'] = {
    init: function() {
        this.appendDummyInput()
            .appendField("创建UDP套接字");
        this.setOutput(true, "Socket");
        this.setColour(color.networking);
        this.setTooltip("创建一个基于udp的网络套接字");
        this.setHelpUrl("");
    }
};

Blockly.Python['create_udp_socket'] = function(block) {
    Blockly.Python.definitions_['import_socket'] = 'import socket';
    var code = 'socket.socket(socket.AF_INET, socket.SOCK_DGRAM)';
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Blocks['socket_connect'] = {
    init: function() {
        this.appendValueInput("SOCKET")
            .setCheck("Socket")
            .appendField("将套接字连接到");
        this.appendValueInput("HOST")
            .setCheck("String")
            .appendField("地址和");
        this.appendValueInput("PORT")
            .setCheck("Number")
            .appendField("端口");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour("#854836"); // 替换为正确的颜色值
        this.setTooltip("将套接字连接到指定的地址");
        this.setHelpUrl("");
    }
};

Blockly.Python['socket_connect'] = function(block) {
    var value_socket = Blockly.Python.valueToCode(block, 'SOCKET', Blockly.Python.ORDER_ATOMIC);
    var value_host = Blockly.Python.valueToCode(block, 'HOST', Blockly.Python.ORDER_ATOMIC);
    var value_port = Blockly.Python.valueToCode(block, 'PORT', Blockly.Python.ORDER_ATOMIC);
    // 导入socket模块
    Blockly.Python.definitions_['import_socket'] = 'import socket';
    // 生成Python代码
    var code = value_socket + '.connect((' + value_host+','+value_port + '))\n';
    return code;
};

Blockly.Blocks['socket_connect_ex'] = {
    init: function() {
        this.appendValueInput("SOCKET")
            .setCheck("Socket")
            .appendField("将套接字连接到");
        this.appendValueInput("HOST")
            .setCheck("String")
            .appendField("地址和");
        this.appendValueInput("PORT")
            .setCheck("Number")
            .appendField("端口");
        this.setInputsInline(true);
        this.setOutput(true, "Number");
        this.setColour("#854836"); // 替换为正确的颜色值
        this.setTooltip("将套接字连接到指定的地址");
        this.setHelpUrl("");
    }
};

Blockly.Python['socket_connect_ex'] = function(block) {
    var value_socket = Blockly.Python.valueToCode(block, 'SOCKET', Blockly.Python.ORDER_ATOMIC);
    var value_host = Blockly.Python.valueToCode(block, 'HOST', Blockly.Python.ORDER_ATOMIC);
    var value_port = Blockly.Python.valueToCode(block, 'PORT', Blockly.Python.ORDER_ATOMIC);
    // 导入socket模块
    Blockly.Python.definitions_['import_socket'] = 'import socket';
    // 生成Python代码
    var code = value_socket + '.connect((' + value_host+','+value_port + '))\n';
    return code;
};



